package com.junchao.peng.service.impl;

import com.junchao.peng.mapper.SessionMapper;
import com.junchao.peng.pojo.Session;
import com.junchao.peng.service.SessionService;
import com.junchao.peng.utils.JsonResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author dell
 */
@Service("SessionService")
public class SessionServiceImpl implements SessionService {
    final SessionMapper sessionMapper;
    final RedisTemplate redisTemplate;

    SessionServiceImpl(RedisTemplate redisTemplate, SessionMapper sessionMapper){
        this.sessionMapper = sessionMapper;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public JsonResult listSession(String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return setStatus(sessionMapper.listSession());
    }

    @Override
    public JsonResult listSessionByUserName(String userName, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return setStatus(sessionMapper.listSessionByUserName(userName));
    }

    @Override
    public JsonResult selectSessionBySessionName(String sessionName, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        Date date;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(dateFormat.format(new Date()));
            return new JsonResult(setStatus(sessionMapper.selectSessionBySessionName(sessionName),date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new JsonResult("请联系管理员",500);
    }

    @Override
    public JsonResult deleteSession(String session, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        if (sessionMapper.deleteSession(session)){
            return new JsonResult("删除成功");
        }
        return new JsonResult("删除失败请联系管理员",500);
    }

    @Override
    public JsonResult insertSession(Session session, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        if (sessionMapper.insertSession(session)){
            return new JsonResult("添加成功");
        }
        return new JsonResult("添加失败请联系管理员",500);
    }

    @Override
    public JsonResult updateSession(Session session, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        if (sessionMapper.updateSession(session)){
            return new JsonResult("修改成功");
        }
        return new JsonResult("修改失败请联系管理员",500);
    }

    private boolean getToken(String token){
        boolean flag = false;
        Object redisCode = redisTemplate.opsForValue().get(token);
        if (redisCode != null){
            flag = true;
        }
        return !flag;
    }

    private Session setStatus(Session session, Date date) {
        if ("通过".equals(session.getMeetStatus())){
            Date startTime = session.getStartTime();
            Date endTime = session.getEndTime();
            int i = date.compareTo(startTime);
            int j = date.compareTo(endTime);
            if (i < 0 || i == 0){
                session.setMeetStatus("未开始");
            }else if (j <= 0){
                session.setMeetStatus("进行中");
            }else {
                session.setMeetStatus("已结束");
            }
        }
        return session;
    }

    private JsonResult setStatus(List<Session> sessions) {
        Date date;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(dateFormat.format(new Date()));
            for (Session session : sessions) {
                setStatus(session,date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new JsonResult(sessions);
    }
}
